/************************************************************
 * Code formatted by Thavarajan
 * Time: 11/27/2013 4:11:54 PM
 ************************************************************/

USE tempdb

IF OBJECT_ID('tempdb.dbo.Tblone') IS NOT NULL
BEGIN
    DROP TABLE Tblthree
    
    DROP TABLE TblTwo
    
    DROP TABLE Tblone
END
GO

CREATE TABLE Tblone
(
	oneid     INT PRIMARY KEY
   ,NAME      VARCHAR(100)
)

CREATE TABLE Tbltwo
(
	twoid     INT PRIMARY KEY
   ,oneid     INT CONSTRAINT fk_two FOREIGN KEY REFERENCES tblone(oneid)
   ,NAME 
    VARCHAR(100)
)

CREATE TABLE Tblthree
(
	Threeid1     INT
   ,Threeid2     INT
   ,twoid        INT CONSTRAINT fk_three FOREIGN KEY REFERENCES tbltwo(twoid)
   ,NAME         VARCHAR(100)
   ,CONSTRAINT pk_three PRIMARY KEY(threeid1 ,threeid2)
)

INSERT INTO Tblone
VALUES
  (
    1
   ,'11'
  ), (2 ,'22')

INSERT INTO Tbltwo
VALUES
  (    1   ,1   ,'112'  ), (2 ,2 ,'222'), (3 ,2 ,'221'), (4 ,2 ,'222')

INSERT INTO Tblthree
VALUES
  (1,1,1,'112'), (2 ,1 ,1 ,'222'), (3 ,1 ,1 ,'221'), (4 ,1 ,1 ,'222'),
(1 ,2 ,2 ,'112'), (2 ,2 ,2 ,'222'), (3 ,2 ,2 ,'221'), (4 ,2 ,2 ,'222'),
(1 ,3 ,3 ,'112'), (2 ,3 ,3 ,'222'), (3 ,3 ,3 ,'221'), (4 ,3 ,3 ,'222'),
(1 ,4 ,4 ,'112'), (2 ,4 ,4 ,'222'), (3 ,4 ,4 ,'221'), (4 ,4 ,4 ,'222')

DECLARE @TableName AS VARCHAR(250)='TblOne'
DECLARE @Column AS VARCHAR(250)='Oneid'
DECLARE @Wstmt AS VARCHAR(MAX)=' '+@Column+' = @value';
WITH KeyDetails AS 
(
    SELECT D.name   AS DependentTable
          ,Dc.name  AS DependentColumn
          ,M.name   AS MainTable
          ,Mc.Name  AS MainColumn
          ,fkc.parent_object_id
          ,Fkc.referenced_object_id
    FROM   sys.foreign_key_columns fkc
           INNER JOIN SYS.tables D
                ON  fkc.parent_object_id = D.[object_id]
           INNER JOIN sys.syscolumns DC
                ON  fkc.parent_object_id = DC.id AND
                    fkc.parent_column_id = DC.colid
           INNER JOIN SYS.tables M
                ON  fkc.referenced_object_id = M.[object_id]
           INNER JOIN sys.syscolumns Mc
                ON  fkc.referenced_object_id = MC.id AND
                    fkc.referenced_column_id = MC.colid
),
DeptDetails AS
(
    SELECT CAST(Fkc.DependentTable AS VARCHAR(MAX)) AS TableRelation
          ,fkc.DependentTable
          ,Fkc.DependentColumn
          ,Fkc.MainTable
          ,Fkc.MainColumn
          ,fkc.parent_object_id  AS childID
          ,1                     AS ReLevel
          ,CAST(
               'SELECT 1 FROM '+FKC.DependentTable+' WHERE EXISTS(
 SELECT 1 FROM '+FKC.MainTable+' WHERE '+FKC.MainTable+'.'+FKC.MainColumn+'='+FKC.DependentTable+'.'
              +FKC.DependentColumn+' AND '+@Wstmt+')' 
               AS VARCHAR(MAX)
           )                     AS STMT
    FROM   KeyDetails               Fkc
    WHERE  fkc.referenced_object_id = OBJECT_ID(@TableName)
    UNION ALL
    SELECT CAST(c.TableRelation+'-->'+fkc.DependentTable AS VARCHAR(MAX)) AS TableRelation
          ,fkc.DependentTable
          ,Fkc.DependentColumn
          ,Fkc.MainTable
          ,Fkc.MainColumn
          ,fkc.parent_object_id  AS childID
          ,c.ReLevel+1
          ,CAST(
               'SELECT 1 FROM '+FKC.DependentTable+' WHERE EXISTS(
'+C.STMT+' 
AND '+FKC.MainTable+'.'+FKC.MainColumn+'='+FKC.DependentTable+'.'+FKC.DependentColumn+')' 
               AS VARCHAR(MAX)
           )                     AS STMT
    FROM   KeyDetails fkc
           INNER JOIN DeptDetails c
                ON  fkc.referenced_object_id = c.Childid AND
                    fkc.parent_object_id<>c.childid
)
SELECT STMT
FROM   DeptDetails
       --select * FROM Tblthree
       --WHERE EXISTS(SELECT 1 FROM tbltwo WHERE tbltwo.twoid=Tblthree.twoid AND EXISTS
       --(SELECT 1 FROM Tblone WHERE tblone.oneid = tbltwo.oneid AND tblone.oneid =1))  
       